
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
<html>
<%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<head>
    <base href="<%=basePath%>">

    <meta charset="UTF-8">
    <title align='center'>图书馆管理员工后台</title>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.4.1.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function (){

        //该函数：收起用户输入的条件进行查询，如何将查询结果解析好展示在对应位置
        $("#selectBtn").click(function() {

            var bookName = $("#bookName").val();
            var bookType = $("#bookType").val();
            var bookAuthor = $("#bookAuthor").val();
            $.ajax({
                url: 'queryBook?method=queryBookByCondition',
                data: {
                    bookName: bookName,
                    bookType: bookType,
                    bookAuthor: bookAuthor
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    showBookForPage(data);
                }
            });
        });



        //全选功能实现
        $("#checkAll").click(function () {

            $("#tBody input[type = 'checkbox']").prop("checked", this.checked);
        });

        $("#tBody input[type = 'checkbox']").click( function () {
            if ($("#tBody input[type = 'checkbox']").length == $("#tBody input[type = 'checkbox']:checked").length) {
                $("#checkAll").prop("checked", true);
            } else {
                $("#checkAll").prop("checked", false);
            }
        });


        //借出书籍函数
        $("#borrowBtn").click(function () {

            var number = $("#numberInput").val();
            if(number === "" || number === null || number === undefined){
                alert("请输入调度件数");
                return ;
            }
            //获取所有选中书籍的id
            var checkedIds = $("#tBody input[type = 'checkbox']:checked");
            if(checkedIds.length == 0){
                alert("请选择要借出的书籍");
                return ;
            }
            //获取所有选中对象id
            if (window.confirm("确定借出吗？")) {
                var ids = "";
                $.each(checkedIds, function () {
                    ids += this.value + ',';
                });
                ids = ids.substr(0, ids.length - 1);

                //发送请求
                $.ajax({
                    url: 'borrowBook?method=borrowBookByIds',
                    data: {
                        ids:ids,
                        number:number
                    },
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        alert(data.message);
                        if(data.code==1){
                            $("#selectBtn").click();
                        }
                    }

                });
            }
        });


        //借入功能
        $("#lendBtn").click(function () {
            //获取所有选中书籍的id
            var checkedIds = $("#tBody input[type = 'checkbox']:checked");
            //如果没选的话就显现添加框
            if(checkedIds.length == 0){
                alert("请选择借入的书籍")
            }
            var number = $("#numberInput").val();
            if(number === "" || number === null || number === undefined){
                alert("请输入调度件数");
                return ;
            }
            //获取所有选中对象id
            var ids = "";
            $.each(checkedIds, function () {
                ids += this.value + ',';
            });
            ids = ids.substr(0, ids.length - 1);
            //发送请求
            $.ajax({
                url: 'lendBook?method=lendBookByIds',
                data: {
                    ids:ids,
                    number:number
                },
                type: 'post',
                dataType: 'json',
                success: function (data) {
                    alert(data.message);
                    if(data.code==1){
                        $("#selectBtn").click();
                    }
                }
            });
        });

        //刷新列表的函数
        function showBookForPage (data){
            if(data.code==1){
                //每查询一次，先把上次查询写到表单的结果清理掉
                $("#bookTab tbody").html("");
                //转json字符串
                var json = JSON.stringify(data.retData);
                //jspn字符串转化为json数组
                var jsonArray = JSON.parse(json);
                for (var i = 0; i < jsonArray.length; i++) {
                    $("#bookTab").append('<tbody id="tBody"><tr><td><input type="checkbox" value=' + jsonArray[i].id + '/></td><td>'+jsonArray[i].isbn+'</td><td>'+jsonArray[i].bookName+'</td><td>'+jsonArray[i].typeId+'</td><td>'+jsonArray[i].remaining+'</td><td>'+jsonArray[i].price+'</td><td>'+jsonArray[i].author+'</td></tr></tbody>');
                }
            }
        }
    });




</script>
<h1 align='center'>图书管理普通员工后台</h1 >
<hr>
<!-- 普通职工进入系统后可以进行查询、管理书籍信息，借出，借入等等操作。 -->
<div>
    <form>

        <b>查询书籍,通过条件筛选,支持模糊查询</b>
        <br>
        <div>书名</div>
        <input type="text" id="bookName"/>
        <div>所属类别</div>
        <input type="text" id="bookType"/>
        <div>作者</div>
        <input type="text" id="bookAuthor"/>
        <input type="button" value="查询" id="selectBtn"/>

    </form>
    <hr>
    <br>
</div>
<b>书籍查询详细结果</b>
<br>

<br><br>
<table h1 border="1px" id="bookTab">
    <thead>
    <tr>
        <th><input type="checkbox" id="checkAll"/>选择</th>
        <th>ISBN</th>
        <th>书名</th>
        <th>所属类别</th>
        <th>剩余件数</th>
        <th>价格</th>
        <th>作者</th>
    </tr>
    </thead>
</table><br>
<br><button type="button" id="borrowBtn">借出</button>&nbsp;
<button type="button" id="lendBtn">借入</button>&nbsp;
调度件数<input type="text" size="5px" id="numberInput"/>&nbsp;

<br>
<a href="/exit">[安全退出]</a>
</body>
</html>